package com.dys.controller;

import com.dys.entity.Department;
import com.dys.service.DepartmentService;
import com.dys.service.impl.DepartmentServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: 疾风
 * @ClassName: DepartmentServlet
 * @Description: 部门Servlet
 * @CreateDate: 2024/10/11
 * @Version: v1.0.0
 */
@WebServlet(urlPatterns = "/offlineSchool/departmentServlet.do")
public class DepartmentServlet extends HttpServlet {
    private final DepartmentService departmentService = new DepartmentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("findDropDownToSelectAllDepartment".equals(method)) {
            findDropDownToSelectAllDepartment(req, resp);
        }
    }

    /**
     * 下拉选择所有班级信息
     */
    protected void findDropDownToSelectAllDepartment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-----下拉选择所有部门信息-----");
        // 调用service层
        List<Department> list = departmentService.findDropDownToSelectAllDepartment();
        // 转为Json
        String classJson = new Gson().toJson(list);
        list.stream().forEach(System.out::println);
        // 响应-因为前台发送的是ajax请求，所以这里响应的是json字符串
        resp.getWriter().println(classJson);
    }
}
